<template>
	<view>
		<view class="bindTit">绑定手机</view>
		<view class="loginForm hasMargTop">
			<view class="loginItem hasFlex posRel">
				<view class="iconView">
					<view class="iconfont icon-shoujihao"></view>
				</view>
				<input type="tel" class="input" name="phoneNum" v-model="phoneNum" placeholder="请输入手机号码" maxlength="11" placeholder-class="holderInp"/>
			</view>
			<view class="loginItem hasFlex posRel vercodeItem">
				<view class="iconView">
					<view class="iconfont icon-yanzhengma"></view>
				</view>
				<input type="number" class="input" name="vercode" v-model="vercode" placeholder="请输入验证码" placeholder-class="holderInp"/>
				<view class="getVercode">
					<view class="lineView"></view>
					<block v-if="isClickFlag">
						<view class="againTxt">重新获取({{countData}}s)</view>
					</block>
					<block v-else>
						<view class="getTxt" @tap="getVercode">获取验证码</view>
					</block>
				</view> 
			</view>
			<button class="loginBtn" :disabled="isDisabled" @tap="goLogin">{{currBtnTxt}}</button>
		</view>
	</view>
</template>

<script>
	var count = 60;
	import { sendMsgCode,wxUserLogin } from '@/api/authLogin'
	export default {
		data() {
			return {
				currBtnTxt:'登录',
				phoneNum:'',
				vercode:'',
				isDisabled:false,
				isClickFlag :  false,
				countData : 60,
				timer : null,
				userInfo:null,
				loadOpt:'',
				canRefreshFlag:0 //0上个页面不刷新接口 1上个页面刷新接口 
			}
		},
		onLoad(options){
			this.loadOpt = options.loadOpt;
			this.canRefreshFlag = options.canRefreshFlag;
			this.userInfo = uni.getStorageSync('loginField');
		},
		onUnload(){
			uni.removeStorageSync('loginField');
		},
		methods: {
			//获取验证码
			getVercode(){
				if(this.phoneNum == ''){
					this.util.showToast('请输入手机号码');
				}else if(!this.regX.regPhone.test( this.phoneNum )){
					this.util.showToast('手机号码格式不对');
				}else{
					this.isClickFlag = true;
					this.sendMsgCode();
					this.timer = setInterval(()=>{
						count-=1;
						this.countData = count;
						if(this.countData == 0){
							count = 60;
							clearInterval(this.timer);
							this.countData = 60;
							this.isClickFlag = false;
						}
					},1000);
				}
			},
			sendMsgCode(){
				uni.showLoading({title:'发送中...', mask: true });
				var content = {mobile:this.phoneNum,sign:"hxWL_@1$2$3"};
				var contentStr = this.crypto.encrypt(JSON.stringify( content ));
				var field = {content:contentStr}
				sendMsgCode(field).then(res=>{}).catch(err=>{});
			},
			goLogin(){
				if(this.phoneNum == ''){
					this.util.showToast('请输入手机号码');
				}else if(!this.regX.regPhone.test( this.phoneNum )){
					this.util.showToast('手机号码格式不对');
				}else if(this.vercode == ''){
					this.util.showToast('请输入手机验证码');
				}else{
					this.isDisabled = true;
					uni.showLoading({ title: '登录中...', mask: true });
					var tmpField = {mobile:this.phoneNum,opt:'visitor',mobileCode:this.vercode,client:'applet'}
					Object.assign(this.userInfo,tmpField);
					wxUserLogin(this.userInfo).then(res=>{
						//console.log(res)
						this.User.token = res.data.datas.token;
						this.User.userIdPss = res.data.datas.userId;
						uni.setStorageSync("token", this.User.token);
						uni.setStorageSync("userIdPss",this.User.userIdPss);
						uni.setStorageSync('logAcc',res.data.datas.account);
						uni.setStorageSync('userPortrait',res.data.datas.userPortrait);
						uni.setStorageSync('nickName',res.data.datas.netName);
						uni.removeStorageSync('loginField');
						uni.showToast({ title: '登录成功' });
				
						setTimeout(()=>{
							if(this.loadOpt == ''){
								uni.switchTab({
									url:'/pages/index/index'
								})
							}else if(this.loadOpt == 'userFixNoRef'){
								uni.navigateBack({
									delta:2
								})
							}else{//userFix
								if(this.canRefreshFlag == 1){
									let pages = getCurrentPages();
									let prevPage = pages[pages.length - 3];
									prevPage.$vm.isRefreshFlag = true;
								}
								uni.navigateBack({
									delta:2
								})
							}
							uni.switchTab({
								url:'/pages/index/index'
							})
						},1500);
					}).catch(err=>{
						this.isDisabled = false;
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	@import '@/static/scss/login.scss';
	page{background: #fff;}
	.bindTit{
		width: 660rpx;
		font-size: 34rpx;
		color: $pss-text-color3;
		padding:20rpx 0;
		font-weight: bold;
		margin:100rpx auto 0;
	}
</style>
